package stu.day25_day32.day25.day2;

import stu.day25_day32.day25.all.TreeNode;

import java.util.HashMap;
import java.util.Map;


public class LeetCode106 {

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        Map<Integer,Integer> map = new HashMap<>();
        int num = 0;
        for(int i : inorder){
            map.put(i,num);
            num += 1;
        }

        return inlast(map,postorder,postorder.length-1,0,postorder.length-1);
    }

    public TreeNode inlast(Map<Integer,Integer> map, int[] preorder, int tag, int begin, int end){
        if(begin>end){
            return null;
        }

        TreeNode node = new TreeNode(preorder[tag]);
        int index = map.get(preorder[tag]);

        node.left  = inlast(map,preorder,tag-(end-index)-1, begin,index-1);
        node.right = inlast(map,preorder,tag-1, index+1, end);

        return node;


    }



}
